package artid.com.arttogoa.fragment;

import java.util.ArrayList;
import java.util.List;
import artid.com.arttogoa.OrderInfo_Activity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.adapter.TransactionManagement_Adapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.TransactionManagementParser;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：TransactionManagement_Fragment   
 * 类描述   ：交易管理Fragment
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-19 下午3:51:37   
 *
 */
public class TransactionManagement_Fragment extends Fragment implements OnClickListener{
	private View v;
	private Context mContext;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private TransactionManagement_Adapter adapter;
	private int whichActivity=0;//默认全部订单(0：全部  1：待发货 2：买家待付款 3：已发货 4：已出售 5：已关闭)     
	private LinearLayout noDataLL;
	private TextView noDataHint;
	private List<TransactionManagementParser> list;
	private List<TransactionManagementParser> more_list;

	public TransactionManagement_Fragment(int whichActivity) {
		super();
		this.whichActivity = whichActivity;
	}


	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v=inflater.inflate(R.layout.fragment_transactionmanagement, null);
		mContext=getActivity();
		initView();
		return v;
	}

	private void initView(){
		list=new ArrayList<TransactionManagementParser>();
		more_list=new ArrayList<TransactionManagementParser>();
		noDataLL=(LinearLayout) v.findViewById(R.id.include_noDataLL);
		noDataHint=(TextView) v.findViewById(R.id.include_noData_hint);
		setNoDataHint();
		mListView =(PullableListView) v.findViewById(R.id.fragment_transactionmanagement_listivew);
		mPullLayout = (PullToRefreshLayout)v.findViewById(R.id.fragment_transactionmanagement_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//上拉加载
				page++;
				getData();
			}
		}); 
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				Intent i=new Intent(mContext,OrderInfo_Activity.class);
				if(whichActivity!=0){
					i.putExtra("whichActivity", whichActivity);
				}else{
					//全部的时候根据订单状态来走
					String order_state=more_list.get(position).getOrder_state();
					if("5".equals(order_state)){
						//全部
						i.putExtra("whichActivity", 0);
					}else if("20".equals(order_state)){
						//待发货
						i.putExtra("whichActivity", 1);
					}else if("10".equals(order_state)){
						//买家待付款
						i.putExtra("whichActivity", 2);
					}else if("30".equals(order_state)){
						//已发货
						i.putExtra("whichActivity", 3);
					}else if("40".equals(order_state)){
						//已出售
						i.putExtra("whichActivity", 4);
					}else if("0".equals(order_state)){
						//已关闭
						i.putExtra("whichActivity", 5);
					}
				}
				i.putExtra("order_id", more_list.get(position).getOrder_id());
				mContext.startActivity(i);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	/**
	 * 设置不同状态的数据为空的显示信息
	 */
	private void setNoDataHint(){
		if(whichActivity==0){
			//全部
			noDataHint.setText("哎呦！没有物品哦！");
		}else if(whichActivity==1){
			//待发货
			noDataHint.setText("哎呦！没有待发货的物品哦！");
		}else if(whichActivity==2){
			//买家待付款
			noDataHint.setText("哎呦！没有买家待付款的物品哦！");
		}else if(whichActivity==3){
			//已发货
			noDataHint.setText("哎呦！没有已发货的物品哦！");
		}else if(whichActivity==4){
			//已出售
			noDataHint.setText("哎呦！没有已出售的物品哦！");
		}else if(whichActivity==5){
			//已出售
			noDataHint.setText("哎呦！没有已出售的物品哦！");
		}
	}

	private void getData(){
		String type="5";//默认全部
		if(whichActivity==0){
			//全部
			type="5";
		}else if(whichActivity==1){
			//待发货
			type="2";
		}else if(whichActivity==2){
			//买家待付款
			type="1";
		}else if(whichActivity==3){
			//已发货
			type="3";
		}else if(whichActivity==4){
			//已出售
			type="4";
		}else if(whichActivity==5){
			//已关闭(也叫已取消)
			type="0";
		}
		VolleyBasic.getInstance(getActivity()).post(true,false, Cache.orderList,
				VolleyMap.getInstance().orderList(type, page,10),
				TransactionManagementParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				VolleyBasic.disMissLoding();
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<TransactionManagementParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				if (adapter == null) {
					adapter=new TransactionManagement_Adapter(mContext, more_list, whichActivity);
					mListView.setAdapter(adapter);
				}
				adapter.notifyDataSetChanged();
			}
			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				VolleyBasic.disMissLoding();
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}

	/**
	 * 防止控件没加载出来报空
	 */
	private void getDatas(){
		if(v!=null){
			page=1;
			isRefreshed=true;
			adapter=null;
			getData();
		}else{
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					if(v!=null){
						getDatas();
					}
				}
			}, 1000);
		}
	}

	/**
	 * 每次切换Fragment刷新页面
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		// TODO Auto-generated method stub
		super.setUserVisibleHint(isVisibleToUser);
		// 每次切换fragment时调用的方法
		if (isVisibleToUser) {
			getDatas();
		}
	}
	
}
